1 /**
2  * Prevents modifications until x units have occured
3  * 
4  * License:
5  *              Copyright Devisualization (Richard Andrew Cattermole) 2014 - 2017.
6  *     Distributed under the Boost Software License, Version 1.0.
7  *        (See accompanying file LICENSE_1_0.txt or copy at
8  *              http://www.boost.org/LICENSE_1_0.txt)
9  */
10 module devisualization.util.algorithms.modification_counter;
11 
12 struct ModifierCounter(T) {
13 	private {
14 		T newValue;
15 		T currentValue;
16 
17 		ushort counter;
18 		ushort counterSetTo;
19 	}
20 
21 	this(ushort resetCounterTo) {
22 		counterSetTo = resetCounterTo;
23 	}
24 
25 	@property {
26 		T get() {
27 			return currentValue;
28 		}
29 
30 		void set(T v, bool immediate=false) {
31 			import std.traits : isPointer, isSomeFunction;
32 			newValue = v;
33 
34 			if (immediate) {
35 				currentValue = newValue;
36 				counter = 0;
37 			} else {
38 				static if (isPointer!T || isSomeFunction!T) {
39 					if (currentValue !is v)
40 						counter = counterSetTo;
41 				} else {
42 					if (currentValue != v)
43 						counter = counterSetTo;
44 				}
45 			}
46 		}
47 
48 		void mark() {
49 			if (counter >= 1) {
50 				counter--;
51 			} else {
52 				currentValue = newValue;
53 			}
54 		}
55 	}
56 }